//
//  PushGroupNoticeViewController.m
//  Findif
//
//  Created by Mac on 2019/2/26.
//  Copyright © 2019年 Cicel Chen. All rights reserved.
//

#import "PushGroupNoticeViewController.h"

@interface PushGroupNoticeViewController ()<UITextViewDelegate>

@property (nonatomic, weak)UITextField * textFiled;
@property (nonatomic, weak)UITextView * textView;
@property (nonatomic, weak)UIButton * finishButton;
@property (nonatomic, weak)UIButton * rightButton;


@end

@implementation PushGroupNoticeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNav];
    [self setupUI];
}


- (void)setupNav{
    
    UIButton * rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35 * SCREENH6, 15 * SCREENH6)];
    
    [rightButton setTitle:@"删除" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didDeleteButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    
    self.rightButton = rightButton;
    
    UIBarButtonItem * rightBar = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.navigationItem.rightBarButtonItem = rightBar;
    
    if (self.model) {
        
        self.title = @"编辑社团公告";
        self.rightButton.hidden = NO;
    }else{
        
        self.title = @"发布社团公告";
        self.rightButton.hidden = YES;
    }
    
}

- (void)setupUI{
    
    self.view.backgroundColor = ColorWithRGB(0xF4F5F6);
    
    UITextField * textFiled = [[UITextField alloc] init];
    
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
//    textFiled.backgroundColor = [UIColor clearColor];
//    textFiled.layer.borderColor = [[UIColor clearColor] CGColor];
    //设置光标颜色
    textFiled.tintColor = ZRBlackColor;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x909090);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"标题（4~16个字）" attributes:attrs];
    textFiled.textColor = ZRBlackColor;
    //    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //    //设置显示模式为永远显示(默认不显示)
    //    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    
    self.textFiled = textFiled;
    
    [self.view addSubview:self.textFiled];
    
    // 创建textView
    UITextView *textView =[[UITextView alloc] init];
    textView.backgroundColor= ZRWhiteColor;
    textView.text = @"正文（10~300字）";
    textView.font = [UIFont systemFontOfSize:14 * SCREENH6];
    textView.textColor = ColorWithRGB(0x909090);
    textView.delegate = self;
    textView.layer.cornerRadius = 5;
    textView.layer.borderColor = [ColorWithRGB(0xF4F5F6) CGColor];
    textView.layer.borderWidth = 1 * SCREENH6;
    textView.layer.masksToBounds = YES;
    
    self.textView = textView;
    [self.view addSubview:self.textView];
    
    
    UIButton * finishButton = [UIButton new];
    
    [finishButton setTitle:@"发布" forState:UIControlStateNormal];
    [finishButton setBackgroundColor:ColorWithRGB(0xFFE035)];
    [finishButton setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    finishButton.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [finishButton addTarget:self action:@selector(clickFinishButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.finishButton = finishButton;
    
    [self.view addSubview:self.finishButton];
    
    if (self.model) {
        
        self.textFiled.text = self.model.notice_title;
        self.textView.text = self.model.notice_content;
    }
}


- (void)viewDidLayoutSubviews{
    
    [self.textFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(topHeight + 10 * SCREENH6);
        make.leading.mas_equalTo(self.view).offset(15 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.width.mas_equalTo(345 * SCREENH6);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.width.mas_equalTo(self.textFiled);
        make.top.mas_equalTo(self.textFiled.mas_bottom).offset(10 * SCREENH6);
        make.height.mas_equalTo(200 * SCREENH6);
    }];
    
    [self.finishButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.textView.mas_bottom).offset(40 * SCREENH6);
        make.width.mas_equalTo(250 * SCREENH6);
        make.height.mas_equalTo(30 * SCREENH6);
    }];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    
    if(textView.text.length < 1){
        textView.text = @"正文（10~300字）";
        textView.textColor = ColorWithRGB(0xbebebe);
    }else{
        
        [textView resignFirstResponder];
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"正文（10~300字）"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.textView resignFirstResponder];
    //    [self.textView endEditing:YES];
}


- (void)clickFinishButton{
    
    if (self.model) {
        
        NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
        
        NSDictionary * dict = @{@"group_id":self.group_id,@"notice_title":self.textFiled.text,@"notice_content":self.textView.text,@"notice_id":ID};
        
        [[ZRNetworkTool sharedTool] editGroupNoticeWithDict:dict WithBlock:^(BOOL success, id responseData) {
            
            if (success) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseData[@"message"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }else{
        
        if (self.textFiled.text.length == 0) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"请填写公告标题"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else if ([self.textView.text isEqualToString:@"正文（10~300字）"]){
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"请输入公告内容"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            NSString * user_id = DEF_GET_OBJECT(@"id");
            
            NSDictionary * dict = @{@"group_id":self.group_id,@"user_id":user_id,@"notice_title":self.textFiled.text,@"notice_content":self.textView.text};
            
            [[ZRNetworkTool sharedTool] addGroupNoticeWithDict:dict WithBlock:^(BOOL success, id responseData) {
                
                if (success) {
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:responseData[@"message"]
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              
                                                                              [self.navigationController popViewControllerAnimated:YES];
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                }else{
                    
                    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                                   message:responseData[@"message"]
                                                                            preferredStyle:UIAlertControllerStyleAlert];
                    
                    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                          handler:^(UIAlertAction * action) {
                                                                              
                                                                          }];
                    
                    [alert addAction:defaultAction];
                    [self presentViewController:alert animated:YES completion:nil];
                }
            }];
        }
    }
    
}


- (void)didDeleteButton{
    
    UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                   message:@"确定删除该公告"
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                              [self deleteNotice];
                                                          }];
    UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction * action) {
                                                              
                                                          }];
    
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}


- (void)deleteNotice{
    
    NSString * ID = [NSString stringWithFormat:@"%@",self.model.id];
    
    NSDictionary * dict = @{@"group_id":self.group_id,@"notice_id":ID};
    [[ZRNetworkTool sharedTool] deleteGroupNoticeWithDict:dict WithBlock:^(BOOL success, id responseData) {
        
        if (success) {
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseData[@"message"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
}

@end
